﻿namespace EAL.Models
{
    using System;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;

    /// <summary>
    /// Description of Customer.
    /// </summary>
    public class Customer : INotifyPropertyChanged, ICsvSerializable    
    {
        int _id;
        String _address;
        String _firstname;
        String _lastname;


        [Key]
        public int Id 
        { 
            get { return _id; }
            set { _id = value; RaisePropertyChanged("Id"); }
        }

        public String Address
        {
            get { return _address; }
            set { _address = value; RaisePropertyChanged("Address"); }
        }

        public String FirstName
        {
            get { return _firstname; }
            set 
            { 
                _firstname = value; 
                RaisePropertyChanged("FirstName"); 
                RaisePropertyChanged("ToString"); 
            }
        }

        public String LastName
        {
            get { return _lastname; }
            set 
            { 
                _lastname = value; 
                RaisePropertyChanged("LastName"); 
                RaisePropertyChanged("ToString"); 
            }
        }

        public string ToCsvLine()
        {
            return String.Format("{0};{1};{2}", this.FirstName, this.LastName, this.Address);
        }

        public ICsvSerializable FromCsvLine(string line)
        {

            String[] data = line.Split(";".ToCharArray());
            return new Customer() { FirstName = data[0], LastName = data[1], Address = data[2] };
        }
        
        [NotMapped]
        public String ToString
        {
            get
            {
                return String.Format("{0} {1}", this.FirstName, this.LastName);
            }
        }

        #region INotifyPropertyChanged implementation
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// raises the property changed event with the given property name
        /// </summary>
        /// <param name="propName">property name to raise event for</param>
        public void RaisePropertyChanged(String propName)
        {
            this.OnPropertyChanged(new PropertyChangedEventArgs(propName));
        }

        /// <summary>
        /// called when a property has cnanged ; calls the event and its associated functors
        /// </summary>
        /// <param name="e">property changed event arguments containing the name of the property</param>
        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            var handler = PropertyChanged;

            if (handler != null)
                PropertyChanged(this, e);
        }
        #endregion
    }
}
